package io.ganguo.chat.route.server.handler;

import io.ganguo.chat.core.connetion.IMConnection;
import io.ganguo.chat.core.handler.IMHandler;
import io.ganguo.chat.core.protocol.Commands;
import io.ganguo.chat.core.protocol.Constant;
import io.ganguo.chat.core.protocol.Handlers;
import io.ganguo.chat.core.transport.Header;
import io.ganguo.chat.core.transport.IMRequest;
import io.ganguo.chat.core.transport.IMResponse;
import io.netty.util.AttributeKey;
import org.springframework.stereotype.Component;

/**
 * Created by hasee on 2016-04-01.
 */
@Component
public class HeartbeatHandler extends IMHandler<IMRequest> {
    @Override
    public short getId() {
        return Handlers.HEARTBEAT;
    }

    @Override
    public void dispatch(IMConnection connection, IMRequest request) {
        Header header = request.getHeader();
        switch (header.getCommandId()) {
            case Commands.SERVER_HEARTBEAT:
                getHeartbeat(connection, request);
                break;
            default:
                connection.close();
                break;
        }
    }

    private void getHeartbeat(IMConnection connection, IMRequest request) {
        connection.getmContext().channel().attr(AttributeKey.valueOf(Constant.HEARTBEAT_PINGED)).set(System.currentTimeMillis());
        System.out.println("心跳检测!");
    }


}
